package com.ruoyi.activiti.controller;

import com.ruoyi.activiti.domain.FormData;
import com.ruoyi.activiti.domain.FormDef;
import com.ruoyi.activiti.service.FormService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@Controller
@RequestMapping("/form")
public class FormController extends BaseController {
    @Autowired
    private FormService formService;

    private String prefix = "form";

    @GetMapping("/design")
    public String design(){
        return prefix + "/design";
    }

    @GetMapping("/design/{formDefId}")
    public String designFormById(@PathVariable("formDefId") Integer formDefId, ModelMap map){
        FormDef formDef = formService.selectFormDefById(formDefId);
        map.put("formData",formDef.getFormJson());
        map.put("formName",formDef.getFormName());
        map.put("formId",formDef.getId());
        return prefix + "/design";
    }

    @GetMapping("/list")
    public String tolist(ModelMap map){
        return prefix + "/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(){
        startPage();
        List<FormDef> formDefs = formService.listFormDef(null);
        return getDataTable(formDefs);
    }

    @GetMapping("data/list")
    public String todatalist(ModelMap map){
        return prefix + "/datalist";
    }

    @PostMapping("data/list")
    @ResponseBody
    public TableDataInfo datalist(){
        startPage();
        List<FormData> formDatas = formService.listFormData(null);
        return getDataTable(formDatas);
    }

    @PostMapping("save")
    @ResponseBody
    public AjaxResult saveFormDef(ModelMap map,Integer formId, @RequestParam(required = true,value = "formData") String jsonForm, @RequestParam(required = true,value = "formName") String formName){
        FormDef formDef = new FormDef();
        formDef.setFormName(formName);
        formDef.setFormJson(jsonForm);
        if(formId!=null && formId>0){
            formDef.setId(formId);
            formService.updateFormDef(formDef);
        }else{
            formService.saveFormDef(formDef);
        }
        return AjaxResult.success();
    }

    @PostMapping("data/save")
    @ResponseBody
    public AjaxResult saveFormData(@RequestParam(required = true,value = "data") String jsonData,Integer formId, String title){
        FormData formData = new FormData();
        formData.setFormData(jsonData);
        formData.setFormDef(formId);
        formData.setTitle(title);
        formData.setCreateBy(ShiroUtils.getLoginName());
        formData.setCreateTime(new Date());
        formService.saveFormData(formData);
        return AjaxResult.success();
    }

    @GetMapping("/show")
    public String showForm(ModelMap map,Integer id){
        FormDef formDef = formService.selectFormDefById(id);
        map.addAttribute("formData",formDef.getFormJson());
        return prefix+"/showForm";
    }
    //开启流程
    @PostMapping("/process/start")
    @ResponseBody
    public AjaxResult startProcess(Integer formId, Integer fromDataId){
        formService.startProcessByForm(formId,fromDataId);
        return AjaxResult.success();
    }

    //办理任务页面
    @GetMapping("/process/complate")
    public String complate(ModelMap map,String taskId){
        FormData formData = formService.getFormDataFromTaskId(taskId);
        map.put("formData",formData);
        map.put("taskId",taskId);
        return "todoitem/complate";
    }

    @ResponseBody
    @PostMapping("/process/complate")
    public AjaxResult submitComplate(String taskId,String formData,Integer formDataId){
        formService.complateByForm(taskId,formData,formDataId);
        return AjaxResult.success();
    }
}
